package AdminTab;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class BackupPolicy {
	 String dURL,	dilogin, dipaswd, dqinput,	dExecute, dExpectedResults,	dActualResults;
	 String[][] xTD, xTC, xTS; //2 dimensional variable to read/Store the test data
		String vKW, vXpath, vTestData;  //Declaring inputs from spreadsheet
		WebDriver myD ;
		
@Before
public void setUp() throws Exception{

	
	 String xPath = "C:\\Priya\\Silverback 4.4\\Automation Test Cases\\Admin\\DevicePrivacyControls\\DevicePrivacyControls.xls";
		
		//Read the entire TC and TS
		xTC = xlReadWrite.xlRead(xPath, "TestCases");
		xTS = xlReadWrite.xlRead(xPath, "TestSteps");
		xTD = xlReadWrite.xlRead(xPath, "TestData");
	
}

	

 @Test
 public void Deviceprivacy() throws Exception {
		
			
			//Go to each row in TC and identify which TC to execute
			for (int h=1; h<xTD.length; h++){  //Go to each row in TD
				if (xTD[h][4].equalsIgnoreCase("Y")){ //is the test Data execution for yes
					//Then read the values of the following local variables data from each row
				dURL =xTD[h][1];
				dilogin = xTD[h][2];
				dipaswd = xTD[h][3];
				dqinput = xTD[h][4];
				
		
					for (int i = 1; i<xTC.length; i++) {   //Go to each row in TC
							if (xTC[i][3].equalsIgnoreCase("Y")) {	//go to every test step if it is y
								xTC[i][4] = "Pass";  //As soon as I identify my test case to execute, it is pass by default on the excel, it updates the excel if the test case/step fails
								for (int j=1; j<xTS.length; j++){ 
								if (xTC[i][0].equalsIgnoreCase(xTS[j][1])){
									
									System.out.println("KW to execute is " + xTS[j][4]);
									System.out.println("xpath to execute is " + xTS[j][5]);
									System.out.println("Data to execute is " + xTS[j][6]);
						
						// call the corresponding KW function
						vKW = xTS[j][4] ;						
						vXpath = xTS[j][5];
						vTestData = xTS[j][6];
						String vError = "Pass"; // This means Before the try is executed it is pass and if it correctly executed all through try, vError will be pass
						//Get the corresponding Test Data value based on the name if the TD variable in TS
						
				 // Function to load the TD values and load into variables. Purpose either take the hardcoded data on the sh
						vTestData = getTDValue(vTestData);
						try{
							//Call the corresponding Keyword function
							if (vKW.equalsIgnoreCase("enterText")) {
								KW2.enterText(vXpath, vTestData);
							}
							if (vKW.equalsIgnoreCase("openBrowser")){
								KW2.openBrowser(vTestData);
								}
							
								if (vKW.equalsIgnoreCase("navigateBrowser")){
								KW2.navigateBrowser(vTestData);
								}
									if(vKW.equalsIgnoreCase("clickElement")){
									KW2.clickElement(vXpath);
									}
										if (vKW.equalsIgnoreCase("verifyText")){
											vError = KW2.verifyText(vXpath, vTestData);
											System.out.println("Test is a" + vError );
										}
										
										if (vKW.equalsIgnoreCase("defaultValue")){
											vError = KW2.defaultvaluelist(vXpath, vTestData);
											System.out.println("Test is a" + vError );
										}
										if (vKW.equalsIgnoreCase("readText")){
											KW2.readText(vXpath);
										
										}									
										
										if (vKW.equalsIgnoreCase("selectList")){
											KW2.selectList(vXpath, vTestData);
										}
										if (vKW.equalsIgnoreCase("checkBox")){
											KW2.checkBox(vXpath);
										}
										if (vKW.equalsIgnoreCase("closeBrowser")){
											KW2.closeBrowser();
										}
										
										if(vKW.equalsIgnoreCase("waitTime")){
											KW.waitTime(2000);
											}
							if (vError.equalsIgnoreCase("Pass")) {
							System.out.println("Pass:" + xTC[i][0] + "_" + vKW); //If it's come this far then pass
							xTS[j][8] ="Pass";
							} else {
								System.out.println("Fail:" + xTC[i][0] + "_" + vKW); //If it's come this far then pass
								xTS[j][8] ="Fail as function returned fail";
								xTS[j][9] = vError;
										

								xTC[i][4] = "Fail";
							}
							
						} catch(Exception myError) {
							System.out.println("Fail:" + xTC[i][0] + "_" + vKW); //Fail if the try loop  fails.
							System.out.println("Error Happened" + myError);  //prints the errors into the logs.
							xTS[j][9] ="Error Happened" + myError;
							xTC[i][4] = "Fail";
						}
						
						
							}
							
						}
					}
					
							//Write back the test Steps with the error
				xlReadWrite.writeXL("C:/Priya/Silverback 4.4/Automation Test Cases/Admin/DevicePrivacyControls/Results/TestCase-"+xTC[i][0]+".xls", "TestCase Results", xTC );
				xlReadWrite.writeXL("C:/Priya/Silverback 4.4/Automation Test Cases/Admin/DevicePrivacyControls/Results/TestStep-"+xTS[i][0]+".xls", "TestSteps Results", xTS );
				
			}	
		}
				
			}
		
			System.out.println("Test Run Complete");
				
			}	
 
  
 
			
			private String getTDValue(String vTestData) {  
		
			if (vTestData.equals("dURL")){
				return dURL;  //returns the value in it
			}
			if (vTestData.equals("dilogin")){
				return dilogin;
			}
			if (vTestData.equals("dipaswd")){
				return dipaswd;
			}
			if (vTestData.equals("dqinput")){
				return dqinput;
		
			}
			
						
			return vTestData;  //If no data found , then it takes the vTestData as is in the Test Steps sheet
		}

			
			
			 @After
				public void tearDown() throws Exception {
					myD.quit();	
				


		
}







